import {Logger} from "./logger";

//https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
export class HashHelper {
    public static hashcode(object: any): number {
        let text = "";
        if (object instanceof String) {
            text = object.toString();
        } else {
            text = JSON.stringify(object);
        }

        let hash = 0, i, chr;

        if (!text || text.length === 0) {
            return hash;
        }

        for (i = 0; i < text.length; i++) {
            chr = text.charCodeAt(i);
            hash = ((hash << 5) - hash) + chr;
            hash |= 0; // Convert to 32bit integer
        }

        Logger.log(`object: ${JSON.stringify(object)}, hashcode: ${hash}`);

        return hash;
    }
}